﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using BookSystem.CommomHelper;
using BookSystem.Model;

namespace BookSystem.BLL
{
    class Borrow
    {

        //借阅记录的获取
        public static List<model.Brrow> getBorrow() {

            List<model.Brrow> brs = new List<model.Brrow>();

            String strSql = "select * from T_brrow";

            DataTable dt = SQLHelper.ExecuteTable(strSql);

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                model.Brrow br = new model.Brrow();
                br.Bid = dt.Rows[i][0].ToString().Trim();
                br.Uid = dt.Rows[i][1].ToString().Trim();
                br.date = dt.Rows[i][2].ToString().Trim();
                brs.Add(br);

            }

            return brs;
        
        
        }

        //按Bid来查询Brrow表中的借阅信息
        public static model.Brrow getBorrowByBid(String Bid)
        {

            model.Brrow b = new model.Brrow();

            String strSql = "select * from T_brrow where Bid = @Bid";

            DataTable dt = SQLHelper.ExecuteTable(strSql, new SqlParameter("@Bid", Bid));

            if (dt.Rows.Count <= 0)
            {

                return b;

            }
            else
            {

                b.Bid = dt.Rows[0][0].ToString().Trim();
                b.Uid = dt.Rows[0][1].ToString().Trim();
                b.date = dt.Rows[0][2].ToString().Trim();

            }

            return b;
        }

        //按Uid来查询Brrow表中的借阅信息
        public static List<model.Brrow> getBorrowByUid(String Uid)
        {

            List<model.Brrow> brs = new List<model.Brrow>();

            String strSql = "select * from T_brrow where Uid = @Uid";

            DataTable dt = SQLHelper.ExecuteTable(strSql, new SqlParameter("@Uid", Uid));

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                model.Brrow br = new model.Brrow();
                br.Bid = dt.Rows[i][0].ToString().Trim();
                br.Uid = dt.Rows[i][1].ToString().Trim();
                br.date = dt.Rows[i][2].ToString().Trim();
                brs.Add(br);

            }

            return brs;
        }


        //借阅记录的增加
        public static bool addBorrow(String Bid, String Uid, String date)
        {

            int i = 0;
            String strSql = "insert into T_brrow values(@Bid, @Uid, @date)";

            i = SQLHelper.ExecuteNonQuery(strSql,
                new SqlParameter("@Bid", Bid),
                new SqlParameter("@Uid", Uid),
                new SqlParameter("@date", date));


            return i > 0 ? true : false;


        }

        //借阅记录的删除
        public static bool delBorrow(String Bid)
        {
            int i = 0;
            String strSql = "delete from T_brrow where Bid = @Bid";

            i = SQLHelper.ExecuteNonQuery(strSql,
                new SqlParameter("@Bid", Bid));


            return i > 0 ? true : false;



        }

        //借阅检测
        public static bool isBorrowed(String Bid)
        {


            String sqlStr = "select * from T_brrow where Bid = @Bid";

            DataTable dt = SQLHelper.ExecuteTable(sqlStr,
                new SqlParameter("@Bid", Bid));

            return dt.Rows.Count > 0 ? true : false;


        }

    }
}
